module.exports = function ($scope, $resource, $modalInstance, toaster, task, updateFlag) {

    init();
    function init(){
        $scope.task = task;
        $scope.fromDate = {
            'updateFlag': updateFlag,
            'oper_list': [
                {'label': 'INSERT', 'check': false},
                {'label': 'UPDATE', 'check': false},
                {'label': 'DELETE', 'check': false}
            ]
        }
        if(updateFlag){
            if(task.oper_type.indexOf('INSERT') != -1){
                $scope.fromDate.oper_list[0].check = true;
            }
            if(task.oper_type.indexOf('UPDATE') != -1){
                $scope.fromDate.oper_list[1].check = true;
            }
            if(task.oper_type.indexOf('DELETE') != -1){
                $scope.fromDate.oper_list[2].check = true;
            }
        } else {
            $scope.task.sync_type = '1';
        }
    }

    $scope.ok = function(){
        if(!$scope.task.task_title){
            toaster.warning({title: '', body: '请填写任务标题'});
            return;
        }

        if(!$scope.task.to_datasource_type){
            toaster.warning({title: '', body: '请填写目标数据库类型'});
            return;
        }
        if(!$scope.task.to_database){
            toaster.warning({title: '', body: '请填写目标数据库名'});
            return;
        }
        if(!$scope.task.to_table_name){
            toaster.warning({title: '', body: '请填写目标表'});
            return;
        }
        var oper = [];
        angular.forEach($scope.fromDate.oper_list, function(item){
            if(item.check){
                oper.push(item.label);
            }
        });
        $scope.task.oper_type = oper.toString();
        if(!$scope.task.oper_type){
            toaster.warning({title: '', body: '请选择操作类型'});
            return;
        }

        $resource('/api/ac/sc/eventTaskService/saveTask', {}, {}).save($scope.task, function(res){
            if (res.errcode === 0){
                if(res.data.id == '0'){
                    toaster.error({title: '', body: '失败, 请重试'});
                } else {
                    toaster.success({title: '', body: '成功'});
                    $scope.task.id = res.data.id;
                    $modalInstance.close($scope.event);
                }
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }
    
    //取消
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    }


};